#ifndef __MODELEDITOR_TOOLBAR_H
#define __MODELEDITOR_TOOLBAR_H

#include "../../JuceLibraryCode/JuceHeader.h"
//==============================================================================
class ModelEditorMainFrame;


class ModelEditorToolbarItemFactory   : public ToolbarItemFactory
{
public:
    ModelEditorToolbarItemFactory() {}
    ~ModelEditorToolbarItemFactory() {}

    //==============================================================================
    // Each type of item a toolbar can contain must be given a unique ID. These
    // are the ones we'll use in this app.
    enum ModelEditorToolbarItemIds
    {
        eFile_open         = 1,
        eFile_close        = 2,
        eFile_import       = 3,
        eFile_export       = 4,
		eFile_exit		   = 5,
        eUI_resourcetree   = 6,
        eUI_showall        = 7,
        eUI_showcontrol    = 8,
        eUI_showproperties = 9,
        eUI_showsetting    = 10,
        eUI_showstats      = 11,
        eHelp_Help         = 12,
    };

    void getAllToolbarItemIds(Array <int>& ids)
    {
        // This returns the complete list of all item IDs that are allowed to
        // go in our toolbar. Any items you might want to add must be listed here. The
        // order in which they are listed will be used by the toolbar customisation panel.

        ids.add(eFile_open);
        ids.add(eFile_close);
        ids.add(eFile_import);
        ids.add(eFile_export);
		ids.add(eFile_exit);
        ids.add(eUI_resourcetree);
        ids.add(eUI_showall);
        ids.add(eUI_showcontrol);
        ids.add(eUI_showproperties);
        ids.add(eUI_showsetting);
        ids.add(eUI_showstats);
        ids.add(eHelp_Help);

        // If you're going to use separators, then they must also be added explicitly
        // to the list.
        ids.add(separatorBarId);
        ids.add(spacerId);
        ids.add(flexibleSpacerId);
    }

    void getDefaultItemSet(Array <int>& ids)
    {
        // This returns an ordered list of the set of items that make up a
        // toolbar's default set. Not all items need to be on this list, and
        // items can appear multiple times (e.g. the separators used here).
        ids.add(eFile_open);
        ids.add(eFile_close);
		ids.add(spacerId);
		ids.add(separatorBarId);
		ids.add(spacerId);
        ids.add(eFile_import);
		ids.add(spacerId);
		ids.add(separatorBarId);
		ids.add(spacerId);
        ids.add(eUI_showall);
		ids.add(spacerId);
		ids.add(eUI_resourcetree);
		ids.add(spacerId);
		ids.add(eUI_showproperties);
		ids.add(spacerId);
		ids.add(eUI_showsetting);
		ids.add(spacerId);
        ids.add(eUI_showcontrol);
		ids.add(spacerId);
		ids.add(separatorBarId);
		ids.add(spacerId);
        ids.add(eUI_showstats);
		ids.add(spacerId);
        ids.add(separatorBarId);
		ids.add(spacerId);
        ids.add(eHelp_Help);
		ids.add(spacerId);
		ids.add(separatorBarId);
		ids.add(eFile_exit);
    }

    ToolbarItemComponent* createItem(int itemId)
    {
        switch(itemId)
        {
            case eFile_open:		return createButtonFromZipFilePNG(itemId, "open", "File_Open.png");
            case eFile_close:		return createButtonFromZipFilePNG(itemId, "close", "File_Close.png");
            case eFile_import:		return createButtonFromZipFilePNG(itemId, "import", "Model_Import.png");
            case eUI_showall:		return createButtonFromZipFilePNG(itemId, "show all", "Show_All.png");
            case eUI_resourcetree:	return createButtonFromZipFilePNG(itemId, "show resource tree", "Resource_Tree.png", "Resource_Tree_toggle.png");
            case eUI_showproperties:return createButtonFromZipFilePNG(itemId, "show properties", "Properties.png", "Properties_toggle.png");
            case eUI_showcontrol:   return createButtonFromZipFilePNG(itemId, "show control panel", "Control.png", "Control_toggle.png");
			case eUI_showsetting:	return createButtonFromZipFilePNG(itemId, "show setting", "Settings.png", "Settings_toggle.png");
			case eUI_showstats:		return createButtonFromZipFilePNG(itemId, "show stats", "Stats.png");
			case eHelp_Help:		return createButtonFromZipFilePNG(itemId, "help", "Help.png");
			case eFile_exit:		return createButtonFromZipFilePNG(itemId, "exit", "Exit.png");
			default:                break;
        }

        return 0;
    }

private:
    StringArray iconNames;
    OwnedArray<Drawable> iconsFromZipFile;

    // This is a little utility to create a button with one of the PNG images in
    // our embedded ZIP file "icons.zip"
    ToolbarButton* createButtonFromZipFilePNG(const int itemId, const String& text, const String& filename, const String& filename_toggle = String::empty)
    {
        if(iconsFromZipFile.size() == 0)
        {
            // If we've not already done so, load all the images from the zip file..
            MemoryInputStream iconsFileStream(BinaryData::icons_zip, BinaryData::icons_zipSize, false);
            ZipFile icons(&iconsFileStream, false);

            for(int i = 0; i < icons.getNumEntries(); ++i)
            {
                ScopedPointer<InputStream> pngFileStream(icons.createStreamForEntry(i));

                if(pngFileStream != 0)
                {
                    iconNames.add(icons.getEntry(i)->filename);
                    iconsFromZipFile.add(Drawable::createFromImageDataStream(*pngFileStream));
                }
            }
        }

        Drawable* image = iconsFromZipFile[iconNames.indexOf(filename)]->createCopy();
        Drawable* image_toggle = NULL;
		if( filename_toggle != String::empty )
			image_toggle = iconsFromZipFile[iconNames.indexOf(filename_toggle)]->createCopy();
		return new ToolbarButton(itemId, text, image, image_toggle);
    }

};

class ToolbarListener :	public Component,
						public ButtonListener
{
public:
   ToolbarListener(ModelEditorMainFrame* mainframe_);
   ~ToolbarListener();

   void resized();
   void buttonClicked(Button* buttonThatWasClicked);

private:
   ScopedPointer<Toolbar>				toolbar;
   ModelEditorToolbarItemFactory		ToolbarFactory;
   ModelEditorMainFrame*				mainframe;
};

#endif